<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单号" prop="orderNo">
      <el-input v-model="dataForm.orderNo" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="物料id" prop="itemId">
      <el-input v-model="dataForm.itemId" placeholder="物料id"></el-input>
    </el-form-item>
    <el-form-item label="物料条码" prop="itemGuid">
      <el-input v-model="dataForm.itemGuid" placeholder="物料条码"></el-input>
    </el-form-item>
    <el-form-item label="数量—最小单位" prop="quantity">
      <el-input v-model="dataForm.quantity" placeholder="数量—最小单位"></el-input>
    </el-form-item>
    <el-form-item label="换算比率" prop="proportion">
      <el-input v-model="dataForm.proportion" placeholder="换算比率"></el-input>
    </el-form-item>
    <el-form-item label="实际完成数量" prop="trueQuantity">
      <el-input v-model="dataForm.trueQuantity" placeholder="实际完成数量"></el-input>
    </el-form-item>
    <el-form-item label="位置code" prop="psCode">
      <el-input v-model="dataForm.psCode" placeholder="位置code"></el-input>
    </el-form-item>
    <el-form-item label="真实位置" prop="areaCode">
      <el-input v-model="dataForm.areaCode" placeholder="真实位置"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderNo: '',
          itemId: '',
          itemGuid: '',
          quantity: '',
          proportion: '',
          trueQuantity: '',
          psCode: '',
          areaCode: ''
        },
        dataRule: {
          orderNo: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ],
          itemId: [
            { required: true, message: '物料id不能为空', trigger: 'blur' }
          ],
          itemGuid: [
            { required: true, message: '物料条码不能为空', trigger: 'blur' }
          ],
          quantity: [
            { required: true, message: '数量—最小单位不能为空', trigger: 'blur' }
          ],
          proportion: [
            { required: true, message: '换算比率不能为空', trigger: 'blur' }
          ],
          trueQuantity: [
            { required: true, message: '实际完成数量不能为空', trigger: 'blur' }
          ],
          psCode: [
            { required: true, message: '位置code不能为空', trigger: 'blur' }
          ],
          areaCode: [
            { required: true, message: '真实位置不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/outorderitem/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderNo = data.outorderitem.orderNo
                this.dataForm.itemId = data.outorderitem.itemId
                this.dataForm.itemGuid = data.outorderitem.itemGuid
                this.dataForm.quantity = data.outorderitem.quantity
                this.dataForm.proportion = data.outorderitem.proportion
                this.dataForm.trueQuantity = data.outorderitem.trueQuantity
                this.dataForm.psCode = data.outorderitem.psCode
                this.dataForm.areaCode = data.outorderitem.areaCode
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/outorderitem/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderNo': this.dataForm.orderNo,
                'itemId': this.dataForm.itemId,
                'itemGuid': this.dataForm.itemGuid,
                'quantity': this.dataForm.quantity,
                'proportion': this.dataForm.proportion,
                'trueQuantity': this.dataForm.trueQuantity,
                'psCode': this.dataForm.psCode,
                'areaCode': this.dataForm.areaCode
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
